package neusoft.edu.cn.a215lmeordering.activities;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import neusoft.edu.cn.a215lmeordering.Listener.Listener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.bean.Person;
import neusoft.edu.cn.a215lmeordering.bean.Update;
import neusoft.edu.cn.a215lmeordering.model.UpdateUserModel;
import neusoft.edu.cn.a215lmeordering.model.UserModel;

public class UpdateUserActivity extends AppCompatActivity {

    private EditText name,pass,phone,address;
    private String name1,pass1,phone1,address1;
    private Button update;

    private Listener listener = new Listener() {
        @Override
        public void onResponse(Object o) {
            Person person = (Person) o;
            name1=person.getUsername();
            phone1=person.getMobilenum();
            address1=person.getAddress();
            name.setText(name1);
            phone.setText(phone1);
            address.setText(address1);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private Listener listener1 = new Listener() {
        @Override
        public void onResponse(Object o) {
            Update update = (Update) o;
            if(update.getSuccess().equals("1")){
                Toast.makeText(UpdateUserActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                finish();
            }else{
                Toast.makeText(UpdateUserActivity.this, "修改失败，请重试", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_update_user);


        init();

        set();

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pass1=pass.getText().toString();
                if(pass1!=null){
                    UpdateUserModel updateUserModel = new UpdateUserModel();
                    updateUserModel.getResultList(Integer.parseInt(LoginActivity.UserId),name1,pass1,phone1,address1,listener1);
                }else{
                    Toast.makeText(UpdateUserActivity.this, "密码不得为空", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void set() {
        UserModel userModel = new UserModel();
        userModel.getResultList(Integer.parseInt(LoginActivity.UserId),listener);
    }

    private void init() {
        name=findViewById(R.id.update_name);
        pass=findViewById(R.id.update_pass);
        phone=findViewById(R.id.update_phone);
        address=findViewById(R.id.update_address);
        update=findViewById(R.id.update_button);
    }

}
